﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using Reddit.JsonParser;

namespace Reddit.EndpointRetriever
{
    class ObjectEndpointRetriever : IEndpointRetriever<object>
    {
        protected HttpEndpointRetriever httpEndpointRetriever;
        protected JavaScriptSerializer jsonSerializer;
        public ObjectEndpointRetriever(HttpEndpointRetriever httpEndpointRetriever)
        {
            this.httpEndpointRetriever = httpEndpointRetriever;
            this.jsonSerializer = new JavaScriptSerializer();
        }
        #region IEndpointRetriever<object> Members

        public object RetrieveEndpoint(Endpoint endpoint)
        {
            string json = this.httpEndpointRetriever.RetrieveEndpoint(endpoint);
            try
            {
                return this.jsonSerializer.DeserializeObject(json);
            }
            catch (ArgumentException ex)
            {
                throw new JsonParseException("Error parsing javascript", ex);
            }
        }

        #endregion
    }
}
